package com.jinzecloud.reportapproval.service.impl;

import com.jinzecloud.reportapproval.bean.Cover;
import com.jinzecloud.reportapproval.service.CoverService;
import com.jinzecloud.reportapproval.utils.R;
import com.jinzecloud.reportapproval.vo.ProductVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jinzecloud.reportapproval.utils.PageUtils;
import com.jinzecloud.reportapproval.utils.Query;

import com.jinzecloud.reportapproval.mapper.ProductMapper;
import com.jinzecloud.reportapproval.bean.Product;
import com.jinzecloud.reportapproval.service.ProductService;
import org.springframework.transaction.annotation.Transactional;


@Service("productService")
public class ProductServiceImpl extends ServiceImpl<ProductMapper, Product> implements ProductService {
    @Autowired
    private CoverService coverService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        QueryWrapper<Product> productQueryWrapper = new QueryWrapper<>();
        String key = (String) params.get("key");
        productQueryWrapper.like("pr_project_number", key)
                .or().like("pr_project_name", key)
                .or().like("pr_producer_model", key)
                .or().like("pr_project_type", key)
                .or().like("selling_area", key)
                .or().like("product_execution_text", key);
        IPage<Product> page = this.page(
                new Query<Product>().getPage(params),
                productQueryWrapper
        );

        return new PageUtils(page);
    }

    @Override
    public List<Product> getProjectNumber() {
        QueryWrapper<Product> queryWrapper = new QueryWrapper<Product>();
        return this.list(queryWrapper);
    }

    @Override
    public Product getProductInfo(String productNumber) {
        QueryWrapper<Product> queryWrapper = new QueryWrapper<Product>().eq("pr_project_number", productNumber);
        return this.getOne(queryWrapper);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public R saveProduct(ProductVo productVo) {
        QueryWrapper<Product> productQueryWrapper = new QueryWrapper<>();
        productQueryWrapper.eq("pr_project_number", productVo.getPrProjectNumber());
        Product product1 = this.getOne(productQueryWrapper);
        if(product1 == null){
            Product product = new Product();
            BeanUtils.copyProperties(productVo, product);
            this.save(product);
            List<Object> coverList = productVo.getCovers();
            List<Cover> collect = coverList.stream().map(item -> {
                Cover cover = new Cover();
                cover.setInfoId(product.getPrId());
                cover.setName(((LinkedHashMap) item).get("name").toString());
                return cover;
            }).collect(Collectors.toList());
            coverService.saveBatch(collect);
        }else {
            return R.error("项目编号已存在");
        }
        return R.ok();
    }

    @Override
    public Product getByIdProduct(Integer prId) {
        Product product = this.getById(prId);

        return null;
    }

    @Override
    public void updateByIdProduct(ProductVo productVo) {
        Product product = new Product();
        BeanUtils.copyProperties(productVo, product);
        this.updateById(product);
        coverService.remove(new QueryWrapper<Cover>().eq("info_id", product.getPrId()));
        List<Object> coverList = productVo.getCovers();
        List<Cover> collect = coverList.stream().map(item -> {
            Cover cover = new Cover();
            cover.setInfoId(product.getPrId());
            cover.setName(((LinkedHashMap) item).get("name").toString());
            return cover;
        }).collect(Collectors.toList());
        coverService.saveBatch(collect);
    }

    @Override
    public ProductVo getByIdProductVo(Integer prId) {
        ProductVo productVo = new ProductVo();
        Product product = this.getById(prId);
        BeanUtils.copyProperties(product, productVo);
        productVo.setCovers(coverService.getCovers(prId));
        return productVo;
    }

    @Override
    public ProductVo getCover(String productNumber) {
        ProductVo productVo = new ProductVo();
        QueryWrapper<Product> productQueryWrapper = new QueryWrapper<>();
        productQueryWrapper.eq("pr_project_number", productNumber);
        Product product = this.getOne(productQueryWrapper);
        BeanUtils.copyProperties(product, productVo);
        productVo.setCovers(coverService.getCovers(product.getPrId()));
        return productVo;
    }

}